package com.hh.bss.talent.interceptor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.project.service.ProjectService;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.StringHelper;

public class CvInterceptor extends HandlerInterceptorAdapter {   
	private static Map cvURLMap=new HashMap();
	private static Map cvDeleteURLMap=new HashMap();
	static{
		
		//项目
		cvURLMap.put("/talent/cvWork/extsave.do", "");
		cvDeleteURLMap.put("/talent/cvWork/extdelete.do", "");
		
		cvURLMap.put("/talent/cvEducation/extsave.do", "");
		cvDeleteURLMap.put("/talent/cvEducation/extdelete.do", "");
		
		cvURLMap.put("/talent/cvCertificate/extsave.do", "");
		cvDeleteURLMap.put("/talent/cvCertificate/extdelete.do", "");
		
		cvURLMap.put("/talent/cvAttachment/extsave.do", "");
		//cvDeleteURLMap.put("/talent/cvAttachment/extdelete.do", ""); //附件的删除，暂时不做处理
		
		cvURLMap.put("/talent/cvTrainning/extsave.do", "");
		cvDeleteURLMap.put("/talent/cvTrainning/extdelete.do", "");
		
		
		
		
	}
	private CvService cvService;
	
	/** 通过spring自动注入 */
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}	
	   @Override  
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {   
//	      String contentType = response.getContentType();   
//	      if (StringUtils.isEmptyOrNullByTrim(contentType)) {   
//	          response.setContentType("text/html;charset=UTF-8");   
//	      }   
		   
		   
		   String pageURI = request.getRequestURI().replaceFirst(request.getContextPath(), "");
		   if (cvDeleteURLMap.containsKey(pageURI)){
			   StringBuffer selectsql=new StringBuffer();
			   String ids=request.getParameter("ids");
			   if ("/talent/cvWork/extdelete.do".equals(pageURI)){				   
				   selectsql.append("select cv_id  from cv_work where id in(").append(ids).append(")");
			   }else if  ("/talent/cvTrainning/extdelete.do".equals(pageURI)){
				   selectsql.append("select cv_id  from cv_trainning where id in(").append(ids).append(")");
			   }else if  ("/talent/cvEducation/extdelete.do".equals(pageURI)){
				   selectsql.append("select cv_id  from cv_education where id in(").append(ids).append(")");
			   }
			   else if  ("/talent/cvCertificate/extdelete.do".equals(pageURI)){
				   selectsql.append("select cv_id  from cv_certificate where id in(").append(ids).append(")");
			   }
			   
			   List l=cvService.getEntityDao().getJdbcDao().findByQuery(selectsql.toString(), new MapObjectRowMapper());
			   StringBuffer sbstr=new StringBuffer(); 
			   for(int i=0;l!=null&&i<l.size();i++){
				   Map m=(Map)l.get(i);
				   if (sbstr.length()>0)
				      sbstr.append(",").append(m.get("cv_id"));
				   else sbstr.append(m.get("cv_id"));
				   
			   }
			   //System.out.println(l.size()+"--"+sbstr.toString()+"before to update*************.."+selectsql.toString());
			   request.setAttribute("ids", sbstr.toString());
		   }
		   
	        return true;   
	    }   
	  
	    @Override  
	    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {   
//	        if (null != modelAndView && null == modelAndView.getModel().get(ConstantsV2.CONTEXT_PATH)) {   
//	                modelAndView.addObject(ConstantsV2.CONTEXT_PATH, request.getContextPath());   
//	        }   
	        String contentType = response.getContentType();  	       
//	        if ( StringHelper.isEmpty(contentType)) {   
//	            response.setContentType("text/html;charset=UTF-8");   
//	        }   
	        String pageURI = request.getRequestURI().replaceFirst(request.getContextPath(), "");
	        String cvId=request.getParameter("cvId");
	        String ids=request.getParameter("ids");
	        try{
		        if (cvURLMap.containsKey(pageURI)&&!StringHelper.isEmpty(cvId)){
		        	System.out.println("has to update..."+cvId);
		        	cvService.updateCvStatus(StringHelper.parseInteger(cvId, 0));
		        	
		        }else if (cvDeleteURLMap.containsKey(pageURI)&&!StringHelper.isEmpty(ids)){
		        	
		        	ids=(String)request.getAttribute("ids");
		        	String[] idarray = ids.split(",");
		        	for (int i = 0; i < idarray.length; i++)
					{
						java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
						
						cvService.updateCvStatus(id);
					}
		        	
		        	
		        }
	        }catch(Exception ex){ex.printStackTrace();}
	        
	        //如果更新人选简历
	        System.out.println("cvInterceptor:"+pageURI);
	        
	        //response.setContentType("text/html;charset=UTF-8");
	    }   
	  
	}  

